<?php
/**
 * Created by PhpStorm.
 * User: xuwenqiang@4399inc.com
 * Date: 2015/11/14
 * Time: 15:22
 */

namespace Application\Base;

class mdlCrud extends mdlBase
{
    public $tableName = "";
    public $idProperty = "";
    public $order = "";
    public $group = "";

    /**
     * 插入数据
     * @param $datas
     * @return bool|int|string
     */
    public function insert($datas)
    {
        return mdlBase::sdb()->insert($this->tableName, $datas, true);
    }

    /**
     * 主键查询单条结果
     * @param $id
     * @param string $field
     * @return array
     */
    public function selectId($id, $field = "*")
    {
        return mdlBase::sdb()->select($this->tableName, $field, $this->idCond($id));
    }

    /**
     * 条件查询多条结果
     * @param $cond
     * @param string $field
     * @return array
     */
    public function selectCond($cond, $field = "*")
    {
        return mdlBase::sdb()->select($this->tableName, $field, $cond);
    }

    /**
     * 查询多条结果
     * @param $cond
     * @param string $field
     * @return array|bool
     */
    public function fetch($cond, $field = "*")
    {
        return mdlBase::sdb()->fetch($this->tableName, $field, $cond);
    }

    /**
     * 主键删除
     * @param $id
     * @return bool|int
     */
    public function delId($id)
    {
        return mdlBase::sdb()->delete($this->tableName, $this->idCond($id));
    }

    /**
     * 条件删除
     * @param $cond
     * @return bool|int
     */
    public function delCond($cond)
    {
        return mdlBase::sdb()->delete($this->tableName, $cond);
    }

    /**
     * 主键更新
     * @param $datas
     * @param $id
     * @return bool
     */
    public function updateId($datas, $id)
    {
        return mdlBase::sdb()->update($this->tableName, $datas, $this->idCond($id));
    }

    /**
     * 条件更新
     * @param $datas
     * @param $cond
     * @return bool
     */
    public function updateCond($datas, $cond)
    {
        return mdlBase::sdb()->update($this->tableName, $datas, $cond);
    }

    /**
     * 组成ID的条件
     * @param $id
     * @return string
     */
    public function idCond($id)
    {
        if (is_numeric($id)) {
            return $this->idProperty . "={$id}";
        } else {
            return $this->idProperty . "='" . $id . "'";
        }
    }


}